import store from "@/store";
import API from "@/api";
import type { ILessonResult } from "@/pages/lowPricedCourse/trialCourseHome/courseList/index.vue";
import type { IDay_course } from "@/pages/lowPricedCourse/trialCourseHome/trialCourse/index.vue";

import { requestNecessaryData as requestNecessaryData1 } from "@/pages/lowPricedCourse/listeningPractice/listenTraining/index";
import { requestNecessaryData as requestNecessaryData3 } from "@/pages/lowPricedCourse/rolePlaying/rolePlay/index";
import { requestDialogueData } from "@/pages/lowPricedCourse/dialogueOriginText/index";
import { requestSceneSpeakData } from "@/pages/lowPricedCourse/situationalDialog/sceneSpeak/index";
import { requestImitationData } from "@/pages/lowPricedCourse/speakingImitation/oralImitation/index";
import { requestOverViewData } from "@/pages/lowPricedCourse/reviewOverview/overview/index";
import { requestSoundExplanationData } from "@/pages/lowPricedCourse/soundExplanation/skillPractice/index";

export const handleSetCallFunction = (type: number) => {
  let lessonPage = "";
  let func: ((section_id: number) => Promise<ILessonResult>) | null = null;

  switch (type) {
    case 0:
      func = requestDialogueData;
      lessonPage = "/pages/lowPricedCourse/dialogueOriginText/dialogueText/index";
      break;
    case 1:
      func = requestNecessaryData1;
      lessonPage = "/pages/lowPricedCourse/listeningPractice/listenTraining/index";
      break;
    case 2:
      func = requestImitationData;
      lessonPage = "/pages/lowPricedCourse/speakingImitation/oralImitation/index";
      break;
    case 3:
      // @ts-ignore
      func = requestNecessaryData3;
      lessonPage = "/pages/lowPricedCourse/rolePlaying/rolePlay/index";
      break;
    case 4:
      func = requestSceneSpeakData;
      lessonPage = "/pages/lowPricedCourse/situationalDialog/sceneSpeak/index";
      break;
    case 5:
      func = requestOverViewData;
      lessonPage = "/pages/lowPricedCourse/reviewOverview/overview/index";
      break;

    default:
      break;
  }

  return {
    func,
    lessonPage,
  }
}

export const handleSetCallFunctionByDateType = (courseItem: IDay_course) => {
  const { data_type, process, day_num } = courseItem;

  let lessonPage = "";
  let func: ((id: number, process: number) => Promise<ILessonResult>) | any = null;

  switch (data_type) {
    case 4:
      func = requestImitationData;
      lessonPage = "/pages/lowPricedCourse/speakingImitation/oralImitation/index";
      break;
    case 5:
      if (process < 100) {
        func = requestNecessaryData3;
        lessonPage = "/pages/lowPricedCourse/rolePlaying/rolePlay/index";
      } else {
        func = async (id: number) => {
          return {
            data_id: id,
            day_num,
            isRelearn: true,
          }
        }
        lessonPage = "/pages/lowPricedCourse/situationalDialog/scoreOverall/index";
      }
      break;
    case 10:
      func = requestSoundExplanationData;
      lessonPage = "/pages/lowPricedCourse/soundExplanation/skillPractice/index";
      break;
    case 6:
      if (process < 100) {
        func = requestSceneSpeakData;
        lessonPage = "/pages/lowPricedCourse/situationalDialog/sceneSpeak/index";
      } else {
        func = async (id: number) => {
          return {
            data_id: id,
            day_num,
            isRelearn: true,
          }
        }
        lessonPage = "/pages/lowPricedCourse/situationalDialog/scoreOverall/index";
      }
      break;

    default:
      break;
  }

  return {
    func,
    lessonPage,
  }
}

export const handleNavigateTo = async (params: IDay_course) => {
  const { data_id, data_type, free_talk_id, speak_skill_id, process, isReLaunch = false } = params;

  let courseStep = -1;
  let section_id = data_id;
  const { func, lessonPage } = handleSetCallFunctionByDateType(params);

  if (data_type === 4) {
    courseStep = 2;
  } else if (data_type === 5) {
    courseStep = 3;
  } else if (data_type === 6) {
    courseStep = 4;
  } else if (data_type === 9) {
    section_id = free_talk_id;
  } else if (data_type === 10) {
    const result = await API.skillData({ id: speak_skill_id }) as any;
    section_id = result.session_id;
  }

  if (func) {
    func(data_id, process).then((res: any) => {
      const temp = Object.assign(res, params);
      store.dispatch("setCurrentDataId", data_id);

      if (isReLaunch) {
        uni.reLaunch({
          url: `${lessonPage}?section_id=${section_id}&data=${encodeURIComponent(JSON.stringify(temp))}&courseStep=${courseStep}&isSynthesis=false`,
        });
      } else {
        uni.navigateTo({
          url: `${lessonPage}?section_id=${section_id}&data=${encodeURIComponent(JSON.stringify(temp))}&courseStep=${courseStep}&isSynthesis=false`,
        });
      }
    })
  }
}